﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Manage_admin_user : AdminPageBase
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        // CheckAdmin();
        if (!IsPostBack)
        {
            BindUser();
           
        }
    }

    protected void CheckAdmin()
    {
        if (Session["ADMIN"] == null)
        {
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }


    }
    protected void BindUser()
    {
        string strWhere = "1=1 ";
        DataSet ds = jtDataAccess.GetList("User", "UserId,Name,Code", "Name", "", pager1.PageSize, pager1.CurrentPageIndex, 1, strWhere);
        dguser.DataSource = ds.Tables[0];
        dguser.DataBind();
    }

    protected void btnDeleteMass_Click(object sender, EventArgs e)
    {
        string strID = "";

        DataTable user = new DataTable();
        user.Columns.Add("UserId", typeof(int));
        user.Columns.Add("NAME", typeof(string));
        DataRow UserRow;

        for (int i = 0; i < dguser.Items.Count; i++)
        {
            if (dguser.Items[i].ItemType == ListItemType.AlternatingItem || dguser.Items[i].ItemType == ListItemType.Item)
            {
                CheckBox chk = (CheckBox)dguser.Items[i].FindControl("chkID");
                HiddenField hiduserId = (HiddenField)dguser.Items[i].FindControl("hdfUserId");
                if (chk.Checked == true && hiduserId != null && hiduserId.Value != "")
                {
                    UserRow = user.NewRow();
                    UserRow["UserId"] = hiduserId.Value;
                    user.Rows.Add(UserRow);
                }
            }
        }

        user.AcceptChanges();
        int flag = jtDataAccess.uspBatchDelUser(user);

        switch (flag)
        {
            case -1:
                JsHelper.Alert("该用户不允许删除！");
                break;
            case 0:
                JsHelper.Alert("删除失败！");
                break;

            case 1:
                JsHelper.Alert("删除成功");
                BindUser();
                break;

            default:
                break;
        }
        chkSelect.Checked = false;
    }

    protected void pager1_PageChanged(object sender, EventArgs e)
    {
        BindUser();
    }

    protected void dguser_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string name;
        int userId;
        int flag = 0;
        if (e.CommandName == "Edit")
        {
            Response.Redirect("edit.aspx?id=" + int.Parse(e.CommandArgument.ToString()));
        }
        else if (e.CommandName == "Delete")
        {
            name = ((Label)this.dguser.Items[e.Item.ItemIndex].FindControl("lblname")).Text.Trim();
            userId = int.Parse(e.CommandArgument.ToString());

            flag = jtDataAccess.OpCenter(JTDataAccess.OpType.Delete, userId, name);

            switch (flag)
            {

                case -1:
                    JsHelper.Alert("删除失败,已经在使用，不允许删除！");
                    break;
                case 0:
                    JsHelper.Alert("删除失败。");
                    break;
                default:
                    JsHelper.Alert("操作成功！");
                    break;
            }

        }
        BindUser();
    }

    protected void dguser_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
       
    }

    protected void adduser_Click(object sender, EventArgs e)
    {
        Response.Redirect("edit.aspx");
    }
    protected void btnInput_Click(object sender, EventArgs e)
    {

    }
}